package com.yungu.swift.report.controller;

import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.admin.AdminReportParam;
import com.yungu.swift.report.entity.ReportDriverDay;
import com.yungu.swift.report.service.DriverDayService;
import com.yungu.swift.report.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 司机平均数控制器
 */
@Slf4j
@RestController
@RequestMapping(value = "/report/driverDay")
@Api(value = "司机时长统计", tags = "司机时长统计")
public class DriverDayCtrl {

    @Autowired
    private DriverDayService driverDayService;

    @PostMapping("/queryIndex")
    @ApiOperation(value = "首页统计 — 司机平均时长", httpMethod = "POST")
    public ResponseData<List<ReportDriverDay>> queryIndex(@RequestBody AdminReportParam reportParam) {
        return driverDayService.queryIndex(reportParam);
    }

    @PostMapping("/queryPageIndex")
    @ApiOperation(value = "司机时长/里程统计数据", httpMethod = "POST")
    public ResponseData<Map<String, Object>> queryPageIndex(@RequestBody AdminReportParam reportParam) {
        return driverDayService.queryPage(reportParam);
    }

    @PostMapping("/duration/export")
    @ApiOperation(value = "司机时长统计数据导出", httpMethod = "POST")
    public void durationExport(@RequestBody AdminReportParam reportParam, HttpServletResponse response) throws Exception {
        List<ReportDriverDay> list = this.driverDayService.list(reportParam).getData();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("{}报表,没有统计数据可导出", "司机时长");
        }
        ExcelUtil.download(reportParam, response, list, ReportDriverDay.class, "/template/driver_duration_report.xlsx", "司机时长");
    }

    @PostMapping("/mileage/export")
    @ApiOperation(value = "司机里程统计数据导出", httpMethod = "POST")
    public void mileageExport(@RequestBody AdminReportParam reportParam, HttpServletResponse response) throws Exception {
        List<ReportDriverDay> list = this.driverDayService.list(reportParam).getData();
        if (CollectionUtils.isEmpty(list)) {
            log.warn("{}报表,没有统计数据可导出", "司机里程");

        }
        ExcelUtil.download(reportParam, response, list, ReportDriverDay.class, "/template/driver_mileage_report.xlsx", "司机里程");
    }
}
